using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour 
{
	#region Fields
	public float MinSpeed;
	public float MaxSpeed;
	
	private float MinRotateSpeed = 60;
	private float MaxRotateSpeed = 120;
	
	private float MinScale = 0.8f;
	private float MaxScale = 2f;
	
	private float currentRotationSpeed;
	private float currentScaleX, currentScaleY, currentScaleZ;
	private float currenSpeed;
	private float x, y, z;
	#endregion
	
	#region Properties
	private Transform mTransform;
	#endregion
	
	#region Functions
	// Use this for initialization
	void Start () 
	{
		// Keep a reference to the transfrom
		mTransform = transform;
		
		SePositionAndSpeed();
	}
	
	// Update is called once per frame
	void Update () 
	{
		// Apply the rotation while the object is advancing. The rotation will be just in the X
		float amntRotation = currentRotationSpeed * Time.deltaTime;
		mTransform.Rotate(new Vector3(-1, 0, 0) * amntRotation);
		
		// Apply the movement to the object. It'll be moving downwards the Y axis
		float amntMovement = currenSpeed * Time.deltaTime;
		mTransform.Translate(Vector3.down * amntMovement, Space.World);
		
		
		
		// Check if the projectile is leaving the screen
		if(mTransform.position.y <= -5.25f)
		{
			// Refresh the position
			SePositionAndSpeed();
		}
	}
	
	// Refresh the object position
	public void SePositionAndSpeed()
	{
		// Set the rotation speed
		currentRotationSpeed = Random.Range(MinRotateSpeed, MaxRotateSpeed);
		
		// Set the current scale
		currentScaleX = Random.Range(MinScale, MaxScale);
		currentScaleY = Random.Range(MinScale, MaxScale);
		currentScaleZ = Random.Range(MinScale, MaxScale);
		
		// Apply the scale
		mTransform.localScale = new Vector3(currentScaleX, currentScaleY, currentScaleZ);
		
		// Set a random current speed
		currenSpeed = Random.Range(MinSpeed, MaxSpeed);
		
		// Prepare the initial position
		y = 6.0f;
		x = Random.Range(-6,6);
		z = 0.0f;
		
		// Apply the initial position
		mTransform.position = new Vector3(x, y, z);
	}
	#endregion
}
